<template>
    <div>
        <!-- Form -->
        <el-button @click="dialogFormVisible = true">添加权限</el-button>
        <el-dialog title="权限信息" :visible.sync="dialogFormVisible" @close="closeDialog">
            <el-form :model="permission">
                <el-form-item label="名称" :label-width="formLabelWidth">
                    <el-input v-model="permission.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="URL" :label-width="formLabelWidth">
                    <el-input v-model="permission.url" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="PID" :label-width="formLabelWidth">
                    <el-input v-model="permission.pid" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图标" :label-width="formLabelWidth">
                    <el-input v-model="permission.icon" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                    <el-input v-model="permission.description" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="顺序" :label-width="formLabelWidth">
                    <el-input v-model="permission.sort" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="savePermission()">确 定</el-button>
            </div>
        </el-dialog>
        <el-table
                :data="permissions"
                style="width: 100%">
            <el-table-column
                    label="编号"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="名称"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="URL"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.url }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="PID"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.pid }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="描述"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.description }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="图标"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.icon }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="顺序"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.sort }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-popconfirm
                            confirm-button-text='好的'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            @confirm="handleDelete(scope.$index, scope.row)"
                            title="确定删除吗？">
                        <el-button size="mini" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :page-size="pageSize"
                @current-change="loadPermissionPage"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "PermissionView",
        data(){
            return{
                permissions:[], //权限集合
                permission:{} , //当前权限
                current: 1,     //当前页数
                pageSize: 10,    //分页长度
                total: 0,        // 数据总个数
                dialogFormVisible: false, //控制对话框的显示
                formLabelWidth: '120px'
            }
        },
        methods:{
            //分页查询
            loadPermissionPage(current){
                this.current = current;
                //调用后台分页查询接口
                this.axios.get("http://localhost:8080/permissions/page?current=" + this.current +"&pageSize=" + this.pageSize)
                    .then(result => {
                        if(result.data.status == "OK"){
                            //获得数据集合
                            this.permissions = result.data.data.records;
                            this.total = result.data.data.total;
                        }
                    });
            },
            //完成回显
            handleEdit(index, row) {
                console.log(index, row);
                this.permission = JSON.parse(JSON.stringify(row));
                this.dialogFormVisible = true;
            },
            //删除
            handleDelete(index, row) {
                console.log(index, row);
                //后台删除权限
                this.axios.delete("http://localhost:8080/permission/" + row.id)
                    .then(result => {
                        if(result.data.status == "OK"){
                            this.loadPermissionPage(this.current);
                        }
                    })
            },
            //添加或更新权限
            savePermission(){
                //通过权限id判断是否进行添加或更新
                if(this.permission.id){
                    //存在id，就更新
                    this.axios.put("http://localhost:8080/permission",
                        this.permission)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadPermissionPage(this.current);
                            }
                        });
                }else{
                    //不存在id，就添加
                    this.axios.post("http://localhost:8080/permission",
                        this.permission)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadPermissionPage(this.current);
                            }
                        });
                }
                this.dialogFormVisible = false;
            },
            //对话框关闭
            closeDialog(){
                this.permission = {};
            }
        },
        mounted(){  //生命周期函数 界面挂载完成
            this.loadPermissionPage(1);
        }
    }
</script>

<style scoped>

</style>